﻿using Admin.Menus;
using Admin.Repository.Menus;
using Admin.Service.Menus.DTO;
using AutoMapper;
using BLL.UserInfos.DTO;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Service.Menus
{
    public class MenuService : IMenuService
    {
        private readonly IMenuRepository menuRepository;
        private readonly IHttpContextAccessor httpContext;
        private readonly IMapper mapper;

        public MenuService(IMenuRepository menuRepository, IHttpContextAccessor httpContext,IMapper mapper)
        {
            this.menuRepository = menuRepository;
            this.httpContext = httpContext;
            this.mapper = mapper;
        }

        /// <summary>
        /// 菜单显示
        /// </summary>
        /// <returns></returns>
        public GetMenuDto<MenuDto> GetMenu()
        {
            GetMenuDto<MenuDto> menuDto = new GetMenuDto<MenuDto>();
            menuDto.Data = TreeMenus(0);
            //var list = menuDto.Data = TreeMenus(0);
            //var data = list.Skip((page - 1) * size).Take(size).ToList();
            //return new GetMenuDto<MenuDto> { Data=data,TotalCount=};
            return menuDto;
        }

        /// <summary>
        /// 菜单显示 递归
        /// </summary>
        /// <returns></returns>
        public List<MenuDto> TreeMenus(int mid)
        {
            var menu = new List<MenuDto>();

            //查询 通过mid查询
            var node = menuRepository.Queryable().Where(m => m.MId == mid).ToList();

            if (node.Count <= 0)
            {
                return null;
            }

            //循环  foreach循环遍历时，当遍历结果为0时结束循环
            foreach (var item in node)
            {
                //var dy = new MenuDto
                //{
                //    MId = item.MId,
                //    MenuId = item.MenuId,
                //    MainMenuName = item.MainMenuName,
                //    Path = item.Path,
                //    Icon = item.Icon,
                //    CreateDate = item.CreateDate,
                //    CreateName = item.CreateName,
                //    Child = TreeMenus(item.MenuId)//循环里面套循环
                //};

                var entity = mapper.Map<MenuDto>(item);

                entity.Child = TreeMenus(item.MenuId);

                menu.Add(entity);
            }
            return menu;
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public IQueryable<Menu> QueryMenu()
        {
            return menuRepository.Queryable();
        }

        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool CreateMenu(MenuDto dto)
        {
            try
            {
                //Menu menu = new Menu
                //{
                //    CreateName = httpContext.HttpContext.User.Identity.Name,
                //    Path = dto.Path,
                //    Icon = dto.Icon,
                //    CreateDate = DateTime.Now,
                //    MainMenuName = dto.MainMenuName,
                //    MId = dto.MId,
                //    IsShow = dto.IsShow
                //};

                //menuRepository.Create(menu);
                var entity = mapper.Map<Menu>(dto);
                entity.CreateName = httpContext.HttpContext.User.Identity.Name;
                entity.CreateDate = DateTime.Now;
                menuRepository.Create(entity);

                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据ID查询一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Menu GetMenuId(int id) 
        {
            try
            {
                return menuRepository.QueryId(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool UpdMenu(MenuDto dto)
        {
            try
            {
                //Menu menu = new Menu
                //{
                //    CreateName = httpContext.HttpContext.User.Identity.Name,
                //    Path = dto.Path,
                //    Icon = dto.Icon,
                //    CreateDate = DateTime.Now,
                //    MainMenuName = dto.MainMenuName,
                //    MId = dto.MId,
                //    IsShow = dto.IsShow
                //};

                var entity = mapper.Map<Menu>(dto);
                entity.CreateName = httpContext.HttpContext.User.Identity.Name;
                entity.CreateDate = DateTime.Now;

                menuRepository.Update(entity);

                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            return menuRepository.Delete(id);
        }
    }
}
